package com.jlt.web.account;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.jlt.entity.account.Group;
import com.jlt.exception.ServiceException;
import com.jlt.service.account.GroupManager;
import com.jlt.web.CrudActionSupport;
import com.jlt.web.util.Struts2ActionUtils;

/**
 * 组管理Action.
 * 
 * 使用Struts2 convention-plugin annotation定义Action参数.
 * 演示带分页的管理界面.
 * 
 * @author zhoutao
 */
//定义URL映射对应/account/group.action
@Namespace("/account")
//定义名为reload的result重定向到group.action, 其他result则按照convention默认.
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "group.action", type = "redirect") })
public class GroupAction extends CrudActionSupport<Group> {

	private static final long serialVersionUID = 1L;
	private static final String ENTITY_NAME = "组";
	@Autowired
	private GroupManager groupManager;

	//-- 页面属性 --//
	private Long id;
	private Group entity;
	private List<Group> allGroupList;//角色列表
	private Page<Group> page = new Page<Group>(15);//每页15条记录
	//private List<Long> checkedRoleIds; //页面中钩选的角色id列表

	//-- ModelDriven 与 Preparable函数 --//
	public void setId(Long id) {
		this.id = id;
	}

	public Group getModel() {
		return entity;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = groupManager.getGroup(id);
		} else {
			entity = new Group();
			entity.setCreatedBy(SpringSecurityUtils.getCurrentUserName());
			entity.setCreatedTime(new Date());
			entity.setUpdatedBy(SpringSecurityUtils.getCurrentUserName());
			entity.setUpdatedTime(new Date());
		}
	}

	//-- CRUD Action 函数 --//
	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		page = groupManager.searchGroup(page, filters);
		return SUCCESS;
	}
	
	public String chooseList() throws Exception {
		allGroupList = groupManager.getAllGroup();
		return "chooseList";
	}
	
	@Override
	public String input() throws Exception {
		//checkedRoleIds = entity.getRoleIds();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		entity.setUpdatedBy(SpringSecurityUtils.getCurrentUserName());
		entity.setUpdatedTime();
		groupManager.saveGroup(entity);
		message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.SAVE,
				Struts2ActionUtils.SUCCESSFUL, ENTITY_NAME,Struts2ActionUtils.CLOSE_CURRENT);
		Struts2Utils.renderJson(message);
		return null;
	}

	@Override
	public String delete() throws Exception {
		Map<String, String> message=new HashMap<String, String>();
		try {
			groupManager.deleteGroup(id);
			message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.DELETE,
					Struts2ActionUtils.SUCCESSFUL, ENTITY_NAME);
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.DELETE,
					Struts2ActionUtils.FAILURE, ENTITY_NAME);
		}
		Struts2Utils.renderJson(message);
		return null;
	}


	//-- 页面属性访问函数 --//
	/**
	 * list页面显示组分页列表.
	 */
	public Page<Group> getPage() {
		return page;
	}

	public List<Group> getAllGroupList() {
		return allGroupList;
	}

	public void setAllGroupList(List<Group> allGroupList) {
		this.allGroupList = allGroupList;
	}

}
